using Godot;
using StarLine.Helpers;
using StarLine.Models;
using System;

public partial class RectCoordinate : Node2D
{
    // 坐标轴一格对应的长度
    [Export]
    public float Interval = 30;

    // 形成线段的顶点A
    [Export]
    public Vector2 PointA = Vector2.Zero;

    // 形成线段的顶点B
    [Export]
    public Vector2 PointB = new Vector2(1, 1);

    // 原始星星
    public Star OriginStar;

    // 对称星星
    public Vector2[] MirrorStar;

    [Export]
    public Color LineColor = Colors.White;

    public RectCoordinate()
    {
        OriginStar = new Star() { Center = new Vector2(3, 1) };
        MirrorStar = new Vector2[OriginStar.Points.Length];
        StarAndMirrorRecalc();
    }

    public void StarAndMirrorRecalc()
    {
        OriginStar.Recalc();
        var count = OriginStar.Points.Length;
        for (int i = 0; i < count; i++)
        {
            var opoint = OriginStar.Points[i];
            var qpoint = MathHelper.GetQ(PointA.X, PointA.Y, PointB.X, PointB.Y, opoint.X, opoint.Y);
            var mpoint = qpoint + (qpoint - opoint);
            MirrorStar[i] = mpoint;
        }
    }

    public override void _Draw()
    {
        base._Draw();
        var globalPos = GlobalPosition;
        var viewportRect = GetViewportRect();

        DrawCrossAndOrigin(globalPos, viewportRect);
        DrawCustomLine(globalPos, viewportRect);
        DrawLine(globalPos, viewportRect);
    }


    ///<summary>绘制原点。</summary>
    private void DrawCrossAndOrigin(Vector2 globalPos, Rect2 viewportRect)
    {
        var localTop = -globalPos.Y;
        var localLeft = -globalPos.X;
        var localBottom = viewportRect.Size.Y - globalPos.Y;
        var localRight = viewportRect.Size.X - globalPos.X;
        DrawLine(new Vector2(localLeft, 0), new Vector2(localRight, 0), LineColor, 1);
        DrawLine(new Vector2(0, localTop), new Vector2(0, localBottom), LineColor, 1);
    }

    private void DrawCustomLine(Vector2 globalPos, Rect2 viewportRect)
    {
        MathHelper.GetLineFormula(PointA.X, PointA.Y, PointB.X, PointB.Y, out var a, out var b, out var c);
        var localLeft = -globalPos.X;
        var localRight = viewportRect.Size.X - globalPos.X;

        var x1 = localLeft;
        var y1 = MathHelper.GetLineFormatValue(a, b, c, x1);
        var x2 = localRight;
        var y2 = MathHelper.GetLineFormatValue(a, b, c, x2);

        var ap1 = Adjust(new Vector2(x1, y1));
        var ap2 = Adjust(new Vector2(x2, y2));

        DrawLine(ap1, ap2, LineColor, 1);
    }

    private void DrawStar(Vector2 globalPos, Rect2 viewportRect)
    {
        for (var i = 0; i < OriginStar.Points.Length; i++)
        {
            var n = (i + 2) % OriginStar.Points.Length;
            var a1 = Adjust(OriginStar.Points[i] * Interval);
            var a2 = Adjust(OriginStar.Points[n] * Interval);
            DrawLine(a1, a2, LineColor, 1);
        }

        for (var i = 0; i < MirrorStar.Length; i++)
        {
            var n = (i + 2) % MirrorStar.Length;
            var a1 = Adjust(MirrorStar[i] * Interval);
            var a2 = Adjust(MirrorStar[n] * Interval);
            DrawLine(a1, a2, LineColor, 1);
        }
    }

    private void DrawLine(Vector2 globalPos, Rect2 viewportRect)
    {
        for (var i = 0; i < OriginStar.Points.Length - 1; i++)
        {
            var n = i + 1;
            var a1 = Adjust(OriginStar.Points[i] * Interval);
            var a2 = Adjust(OriginStar.Points[n] * Interval);
            DrawLine(a1, a2, LineColor, 1);
        }

        for (var i = 0; i < MirrorStar.Length - 1; i++)
        {
            var n = i + 1;
            var a1 = Adjust(MirrorStar[i] * Interval);
            var a2 = Adjust(MirrorStar[n] * Interval);
            DrawLine(a1, a2, LineColor, 1);
        }
    }


    // 因为绘制的坐标轴原点在左上角，所以要将内容变为左下角。
    private Vector2 Adjust(Vector2 vector)
    {
        return new Vector2(vector.X, -vector.Y);
    }
}
